-- --------------------------------------------------------------------------------------------------------------------------
-- >>>      						!admin = admin mode
-- --------------------------------------------------------------------------------------------------------------------------

	AEGIS.chatCommands.admin = {adminOnly=true;};

	function AEGIS.chatCommands.admin:func(player, chatMsg)

				if (XIsAdmin(player)) then
 				if (not XIsProtected(player)) then

  	XSendBigCenterMessageToPlayer(player,"<b><i><font color=\"#FFFFFF\">Admin </font><font color=\"#FF0000\">Mode </font><font color=\"#FFFFFF\">ON</font>");
			XSendChatToAll(nil, "%s has logged into Admin Mode", player:GetName());

			local name = player:GetName();

	XPlayerChangeName(name, XFormat("%s%s", XCfgVar.adminTag, name));else

  	XSendBigCenterMessageToPlayer(player,"<b><i><font color=\"#FFFFFF\">Admin </font><font color=\"#FF0000\">Mode </font><font color=\"#FFFFFF\">OFF</font>");
			XSendChatToAll(nil, "%s has logged out from Admin Mode", player:GetName());

  			local name 	= player:GetName();
  			local newname 	= string.sub(name, string.len(XCfgVar.adminTag)+1, string.len(name));

  	XPlayerChangeName(name, newname);end else
	XSendBigCenterMessageToPlayer(player,"<font color=\"#ff0000\">[</font><font color=\"#FFFFFF\">DU BIST KEIN ADMIN</font><font color=\"#ff0000\">]</font>");end end

-- --------------------------------------------------------------------------------------------------------------------------
-- >>>      						!restart = Starte Map Neu
-- --------------------------------------------------------------------------------------------------------------------------

	AEGIS.chatCommands.restart = {adminOnly=true;};

		function AEGIS.chatCommands.restart:func(player, chatMsg)

    				if (XIsAdmin(player)) then

        XSendBigCenterMessage("<b><i><font color=\"#ff0000\">Server</font><font color=\"#FFFFFF\"> will be Restart </font><font color=\"#ff0000\">Map</font>");

        	Script.SetTimer( 5000,function() System.ExecuteCommand(XFormat("sv_restart")); end);else

        XSendBigCenterMessageToPlayer(player,"<b><i><font color=\"#ff0000\">[</font><font color=\"#FFFFFF\">DU BIST KEIN ADMIN</font><font color=\"#ff0000\">]</font>");end end

-- --------------------------------------------------------------------------------------------------------------------------
-- >>>      						!next = Starte Andere Map
-- --------------------------------------------------------------------------------------------------------------------------

	AEGIS.chatCommands.next = {adminOnly=true;};

		function AEGIS.chatCommands.next:func(player, chatMsg)

    				if (XIsAdmin(player)) then

        XSendBigCenterMessage("<b><i><font color=\"#FF0000\">Server</font><font color=\"#FFFFFF\"> will change to the </font><font color=\"#ff0000\">Next Map</font>");

        	Script.SetTimer( 5000,function() System.ExecuteCommand(XFormat("g_nextlevel")); end);else

        XSendBigCenterMessageToPlayer(player,"<b><i><font color=\"#ff0000\">[</font><font color=\"#FFFFFF\">DU BIST KEIN ADMIN</font><font color=\"#ff0000\">]</font>");end end

-- --------------------------------------------------------------------------------------------------------------------------
-- >>>      						!password = Server Passwort killa
-- --------------------------------------------------------------------------------------------------------------------------

	AEGIS.chatCommands.password = {adminOnly=true;};

		function AEGIS.chatCommands.password:func(player, chatMsg)

    				if (XIsAdmin(player)) then

        XSendBigCenterMessageToPlayer(player, "<font size=\"25\"><b><i><font color=\"#FFFFFF\">DER SERVER HAT JETZT DAS PASSWORT </font><font color=\"#ff0000\">killa</font>");
           XSendChatToTarget(nil, player, "Server is now Password Protected!")

        	Script.SetTimer( 1000,function() System.ExecuteCommand(XFormat("sv_password killa")); end);else

        XSendBigCenterMessageToPlayer(player,"<b><i><font color=\"#ff0000\">[</font><font color=\"#FFFFFF\">DU BIST KEIN ADMIN</font><font color=\"#ff0000\">]</font>");end end

-- --------------------------------------------------------------------------------------------------------------------------
-- >>>      						!nopassword = Server ohne Passwort
-- --------------------------------------------------------------------------------------------------------------------------

	AEGIS.chatCommands.nopassword = {adminOnly=true;};

		function AEGIS.chatCommands.nopassword:func(player, chatMsg)

    				if (XIsAdmin(player)) then

        XSendBigCenterMessageToPlayer(player, "<font size=\"25\"><b><i></font><font color=\"#FFFFFF\">DER SERVER HAT JETZT </font><font color=\"#ff0000\">KEIN PASSWORT</font><font color=\"#FFFFFF\"> MEHR</font>");
           XSendChatToTarget(nil, player, "Server is now without Password!");

        	Script.SetTimer( 1000,function() System.ExecuteCommand(XFormat("sv_password 0")); end);else

        XSendBigCenterMessageToPlayer(player,"<b><i><font color=\"#ff0000\">[</font><font color=\"#FFFFFF\">DU BIST KEIN ADMIN</font><font color=\"#ff0000\">]</font>");end end

-- --------------------------------------------------------------------------------------------------------------------------
-- >>>      						!round = ChatKommandos Rundenzeit aendern
-- --------------------------------------------------------------------------------------------------------------------------

	AEGIS.chatCommands.round = {adminOnly=true;};

		function AEGIS.chatCommands.round:func(player, chatMsg)
	if (XIsAdmin(player)) then
		local roundtime = string.match((chatMsg.text), "^!round (%w+)");
	if (not roundtime) then

		XSendChatToTarget(nil, player, "Please specify Roundtime!");
	else
			local roundtimefinal = "x_time "..roundtime;
		System.ExecuteCommand(roundtimefinal);
		XSendChatToTarget(nil, player, "Server change: New Roundtime is "..roundtime.." Minutes");

	end
else
		XSendBigCenterMessageToPlayer(player, "<font color=\"#ffffff\">Unknown command:</font><font color=\"#ff0000\"> roundtime</font>");
	end
end

-- --------------------------------------------------------------------------------------------------------------------------
-- >>>      						!pointlimit = Score- und Fraglimit aendern
-- --------------------------------------------------------------------------------------------------------------------------

	AEGIS.chatCommands.pointlimit = {adminOnly=true;};

		function AEGIS.chatCommands.pointlimit:func(player, chatMsg)
	if (XIsAdmin(player)) then
		local pointlimit = string.match((chatMsg.text), "^!pointlimit (%w+)");
	if (not pointlimit) then
		if (XVar.currentMapInfo.mapRules=="TeamInstantAction" ) then
			XSendChatToTarget(nil, player, "Please specify Scorelimit!");
			   elseif (XVar.currentMapInfo.mapRules=="InstantAction") then

			XSendChatToTarget(nil, player, "Please specify Fraglimit!");
		else
			XSendChatToTarget(nil, player, "This Command is only for IA and TIA!");
	end
else
	if (XVar.currentMapInfo.mapRules=="TeamInstantAction" ) then
		System.SetCVar("g_scorelimit", pointlimit);
			System.ExecuteCommand("x_say_info Server change: New Scorelimit is "..pointlimit.." Points");

			XSendChatToTarget(nil, player, "New Scorelimit: "..pointlimit.." Points");
	elseif (XVar.currentMapInfo.mapRules=="InstantAction") then
		System.SetCVar("g_fraglimit", pointlimit);
			System.ExecuteCommand("x_say_info Server change: New Fraglimit is "..pointlimit.." Kills");

			XSendChatToTarget(nil, player, "New Fraglimit: "..pointlimit.." Kills");
		end
	   end
      end
end

-- --------------------------------------------------------------------------------------------------------------------------
-- >>>      						!punish
-- --------------------------------------------------------------------------------------------------------------------------

	AEGIS.chatCommands.punish = {adminOnly=true;};

	function AEGIS.chatCommands.punish:func(player, chatMsg)

--===================================
  local StandardPunishTime = 2;
--===================================

        		local xpunish, xtime, xreason = string.match(string.lower(chatMsg.text), "^!punish (%w+)(%d+)(.*)$")

        			if (not xpunish) then
           	XSendChatToTarget(nil, player, "Please specify Playername!");
           return;
     end

        			if (not xtime) then
                xtime = StandardPunishTime;
     end
        		local player = XSimplePlayerForName(xpunish,"!punish");
        		local sender = chatMsg.sender;

         			if (not player) then
           	XSendChatToTarget(nil, sender, "Playername not found: "..xpunish);
          return;
     end

         			if (not xreason) then
           	xreason = "Rulebreaker";
     end

        	XSendChatToTarget(nil, sender, "Player punished: "..player:GetName());
        	XSendChatToTarget(nil, sender, "Reason: "..xreason);

        	XPenaltyBox(player:GetName(),xtime,xreason);
     end

-- --------------------------------------------------------------------------------------------------------------------------
-- >>>      						!unpunish
-- --------------------------------------------------------------------------------------------------------------------------

	AEGIS.chatCommands.unpunish = {adminOnly=true;};

	function AEGIS.chatCommands.unpunish:func(player, chatMsg)

        		local xunpunish = string.match((chatMsg.text), "^!unpunish (%w+)");

         			if (not xunpunish) then
           	XSendChatToTarget(nil, player, "Please specify Playername!");
          return;
     end

        		local xunpunishfinal = XSimplePlayerForName(xunpunish,"x_idontcare");

         			if (not xunpunishfinal) then
           	XSendChatToTarget(nil, player, "Playername not found: "..xunpunish);
          return;
     end

		XSendChatToTarget(nil, player, "Player unpunished: "..xunpunishfinal:GetName());

        	XReleasePenaltyBox(xunpunishfinal:GetName(), false);
     end

-- --------------------------------------------------------------------------------------------------------------------------
-- >>>      						!players = Spielerliste
-- --------------------------------------------------------------------------------------------------------------------------

	AEGIS.chatCommands.players = {adminOnly=true;};

		function AEGIS.chatCommands.players:func(player, chatMsg) local players = System.GetEntitiesByClass("Player");

   				if (XIsAdmin(player)) then

	XSendBigCenterMessageToPlayer(player,"<b><i><font color=\"#ff0000\">[</font><font color=\"#FFFFFF\">SPIELER INFO IST JETZT IN DER CONSOLE</font><font color=\"#ff0000\">]</font>");

      	XSendConsoleMessageToPlayer(player, "-------------------------------- Spieler Liste mit Infos --------------------------------");

      			for index, value in ipairs(players) do

        XSendConsoleMessageToPlayer(player, "Name: %s | Profile-ID: %s| Player-Host: %s | ID: %s ", value:GetName(), value.info.profile, value.info.host, value.info.channel);
   	XSendConsoleMessageToPlayer(player, "----------------------------------------------------------------------------------------");end else

      	XSendBigCenterMessageToPlayer(player,"<b><i><font color=\"#ff0000\">[</font><font color=\"#FFFFFF\">DU BIST KEIN ADMIN</font><font color=\"#ff0000\">]</font>");end end

-- --------------------------------------------------------------------------------------------------------------------------
-- >>>      						!kick = Spieler Kicken
-- --------------------------------------------------------------------------------------------------------------------------

	AEGIS.chatCommands.kick = {adminOnly=true;};

		function AEGIS.chatCommands.kick:func(player, chatMsg)

    				if (XIsAdmin(player)) then

        		local xkick 		= chatMsg.text;
        		local xkickfinal 	= string.sub(xkick, 7);
        		local player 		= XSimplePlayerForName(xkickfinal, "x_idontcare");
        		local sender 		= XGetPlayerByName(chatMsg.sender);

    				if (not player) then

        XSendBigCenterMessageToPlayer(sender, "<b><i><font color=\"#ff0000\">[</font><font color=\"#FFFFFF\">Spieler "..xkickfinal.." nicht wurde nicht gefunden</font><font color=\"#ff0000\">]</font>");return end

    				if (XIsAdmin(player)) then

        XSendBigCenterMessageToPlayer(player,"<b><i><font color=\"#ff0000\">[</font><font color=\"#FFFFFF\">DU KANNST DEN ADMIN NICHT KICKEN</font><font color=\"#ff0000\">]</font>");return end

        		XKick(player:GetName(), 5, "GEKICKT");

        XSendChatToTarget(nil, player, "Kicked "..xkickfinal.." from the Server.");else

        XSendBigCenterMessageToPlayer(player,"<b><i><font color=\"#ff0000\">[</font><font color=\"#FFFFFF\">DU BIST KEIN ADMIN</font><font color=\"#ff0000\">]</font>");end end

-- --------------------------------------------------------------------------------------------------------------------------
-- >>>     						!kicks = Spieler Kicken
-- --------------------------------------------------------------------------------------------------------------------------

	AEGIS.chatCommands.kicks = {adminOnly=true;};

		function AEGIS.chatCommands.kicks:func(player, chatMsg)

				if (XIsAdmin(player)) then

        			local xkicksv 		= chatMsg.text;
        			local xkickfinalsv 	= string.sub(xkicksv, 9);
        			local player 		= XSimplePlayerForName(xkickfinalsv, "x_idontcare");
        			local sender 		= XGetPlayerByName(chatMsg.sender);

    				if (not player) then

	XSendBigCenterMessageToPlayer(sender, "<b><i><font color=\"#ff0000\">[</font><font color=\"#FFFFFF\">Spieler "..xkickfinal.." nicht wurde nicht gefunden</font><font color=\"#ff0000\">]</font>");

        XSendErrorMessageToPlayer(sender, "Player "..xkickfinalsv.." not found.");return end

				if (XIsAdmin(player)) then

	XSendBigCenterMessageToPlayer(sender, "<b><i><font color=\"#ffffff\">You cannot Kick another </font><font color=\"#ff0000\">Administrator.</font>")

        XSendErrorMessageToPlayer(sender, "You cannot Kick another Administrator.");return end FileHnd, Err = io.open("-ADMIN-KICKS.log","a+");

        			if (not FileHnd) then return;end;

            	FileHnd:write("\n");
            	FileHnd:write("--ADMIN SHORT KICK REPORT--            Date Time: "..XTimeStamp().."\n");
            	FileHnd:write("=================================================================\n");
            	FileHnd:write("Admin Name   : "..sender:GetName().."\n");  --\n jumps to next line
            	FileHnd:write("=================================================================\n");
            	FileHnd:write(XFormat("Kicked Player: Name       : %s  \n", player.info.name));
            	FileHnd:write(XFormat("Kicked Player: Profile-ID : %s  \n", player.info.profile));
    		FileHnd:write(XFormat("Kicked Player: ID         : %s  \n", player.info.channel));
    		FileHnd:write(XFormat("Kicked Player: Host       : %s  \n", player.info.host));
            	FileHnd:write(XFormat("Kicked Player: GUID       : %s  \n", player.info.guid));
            	FileHnd:write("=================================================================\n");
            	FileHnd:write("--END OF REPORT--\n");
            	FileHnd:close();

	XSendBigCenterMessage("<b><i><font color=\"#ffffff\"> ", player:GetName(), "</font><font color=\"#ff0000\"> LOGGED<font color=\"#ffffff\"> and</font><font color=\"#ff0000\"> KICKED!</font>");

		System.ExecuteCommand(XFormat("kick %s", player:GetName()));else

      	XSendBigCenterMessageToPlayer(player,"<b><i><font color=\"#ff0000\">[</font><font color=\"#FFFFFF\">DU BIST KEIN ADMIN</font><font color=\"#ff0000\">]</font>");end end

-- --------------------------------------------------------------------------------------------------------------------------
-- >>>      						!ban = Spieler Bannen
-- --------------------------------------------------------------------------------------------------------------------------

	AEGIS.chatCommands.ban = {adminOnly=true;};

		function AEGIS.chatCommands.ban:func(player, chatMsg)

    				if (XIsAdmin(player)) then

        		local xban 		= chatMsg.text;
        		local xbanfinal 	= string.sub(xban, 6);
        		local player 		= XSimplePlayerForName(xbanfinal, "x_idontcare");
        		local sender 		= XGetPlayerByName(chatMsg.sender);

    				if (not player) then

        XSendBigCenterMessageToPlayer(sender, "<b><i><font color=\"#ff0000\">[</font><font color=\"#FFFFFF\">Spieler "..xkickfinal.." nicht wurde nicht gefunden</font><font color=\"#ff0000\">]</font>");return end

    				if (XIsAdmin(player)) then

        XSendBigCenterMessageToPlayer(player,"<b><i><font color=\"#ff0000\">[</font><font color=\"#FFFFFF\">DU KANNST DEN ADMIN NICHT BANNEN</font><font color=\"#ff0000\">]</font>");return end

        XBan(player:GetName(), "Banned by Admin");

        XSendChatToTarget(nil, player, "Banned "..xbanfinal.." from the Server.");else

        XSendBigCenterMessageToPlayer(player,"<b><i><font color=\"#ff0000\">[</font><font color=\"#FFFFFF\">DU BIST KEIN ADMIN</font><font color=\"#ff0000\">]</font>");end end

-- --------------------------------------------------------------------------------------------------------------------------
-- >>>      						!team = Spieler in anderes Team schicken
-- --------------------------------------------------------------------------------------------------------------------------

	AEGIS.chatCommands.team = {adminOnly=true;};

		function AEGIS.chatCommands.team:func(player, chatMsg)

    				if (XVar.currentMapInfo.mapRules=="InstantAction") then

      		XSendChatToTarget(nil, player, "You can only use !team in PS and TIA games!");

     	return;
    end
      		local target = string.match((chatMsg.text), "^!team (.*)$")

    				if (not target) then
      		XSendChatToTarget(nil, player, "Please specify a playername!");

     	return;
    end
    		local check = XSimplePlayerForName(target,"!team");

    				if (not check) then
      		XSendChatToTarget(nil, player, "Player not found!");
     	return;
    end

    XPlayerChangeTeam(target);

end

-- --------------------------------------------------------------------------------------------------------------------------
-- >>>      						!x
-- --------------------------------------------------------------------------------------------------------------------------

	AEGIS.chatCommands.x = {adminOnly = true;}

	function AEGIS.chatCommands.x:func(player, chatMsg)
    		System.ExecuteCommand(string.sub(chatMsg.text, 4))
   end


-- ==========================================================================================================================
-- ==========================================================================================================================
-- 13				           Admin Fahrzeug Spawn
-- ==========================================================================================================================
-- ==========================================================================================================================

AEGIS.chatCommands.spawn = {
	adminOnly = true;
}

function AEGIS.chatCommands.spawn:func(player, chatMsg)
	--System.LogAlways(player:GetName().." requests a "..string.sub(chatMsg.text, 8))
	local 	VehicleList = {
		["vtol"] 	= "US_vtol";
		["heli"]	= "Asian_helicopter";
		["lol"]		= "US_trolley";
		["car"]		= "Civ_car1";
		["truck"]	= "Asian_truck";
		["aaa"]		= "Asian_aaa";
		["apcnk"]	= "Asian_apc";
		["apcus"]	= "US_apc";
		["tank"]	= "US_tank";
		["hover"]	= "US_hovercraft";
		["asv"] 	= "US_asv";
		["ltv"]		= "US_ltv";
		["boot1"]	= "Civ_speedboat";
		["boot2"]	= "Asian_patrolboat";
	};
	if (not XIsAdmin(player)) then
		XSendBigCenterMessageToPlayer(player,"<b><i><font color=\"#ff0000\">[</font><font color=\"#FFFFFF\">DU BIST KEIN ADMIN</font><font color=\"#ff0000\">]</font>");
		return;
	end
	local vehicleName = VehicleList[string.sub(string.lower(chatMsg.text), 8)];
	if (vehicleName) then
		--System.LogAlways("spawning")
		--Systen.LogAlways(player:GetName().." want's to spawn a "..vehicleName)
		XSpawnVehicle(player:GetName(), 8, vehicleName)
	else
		XSendBigCenterMessageTopPlayer(player, "<b><i><font color=\"#ff0000\"Unrecognized Vehicle: "..vehicleName.."</font></i></b>")
	end
end

AEGIS.chatCommands.reports = {
	adminOnly = true;
}

function AEGIS.chatCommands.reports:func(player, chatMsg)
	local detail = tonumber(string.sub(chatMsg.text, 9))
	if (detail and AEGIS.chatCommands.report.reports[detail]) then
	    local report = AEGIS.chatCommands.report.reports[detail]
		XSendConsoleMessageToPlayer(player, "Id: "..report.id.."\tSender: "..report.senderName.."\tStatus: "..report.status)
		XSendConsoleMessageToPlayer(player, "Message: "..report.message)
	else
		XSendConsoleMessageToPlayer(player, "*********************************************REPORTS************************************************************")
		for i, report in pairs(AEGIS.chatCommands.report.reports) do
			XSendConsoleMessageToPlayer(player, "Id: "..report.id.."\tSender: "..report.senderName.."\tStatus: "..report.status)
			if (report.status == "Unseen") then
				report.status = "Seen"
			end
		end
		LogAlways("Done")
	end
end

AEGIS.chatCommands.closereport = {
	adminOnly = true;
}

function AEGIS.chatCommands.closereport:func(player, chatMsg)
	LogAlways("Getting id + msg")
	local id, msg = string.match(string.lower(chatMsg.text), "^!closereport (%d+) (.*)$")
	LogAlways("Got id "..id.." and msg "..msg)
	if (not id) then
		LogAlways("id missing")
	end
	if (not msg) then
		LogAlways("msg missing")
	end
	local lReport, pos
 	for i,report in pairs(AEGIS.chatCommands.report.reports) do
		LogAlways("Looking at "..report.id)
 	    if (tonumber(report.id) == tonumber(id)) then
			LogAlways("Got it")
 	        lReport = report
 	        pos = i
 	        break
		end
	end
	if (lReport) then
		XSendChatToTarget(nil, lReport.sender, player:GetName().." answered your report:")
		XSendChatToTarget(nil, lReport.sender, msg)
		table.remove(AEGIS.chatCommands.report.reports, pos)
	else
		LogAlways("report missing")
	end
end


System.LogAlways("Admin.all.lua succesfully loaded")